# -*- coding: utf-8 -*-
from scpy.logger import get_logger
import os
import sys
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice, TagExtractor
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.cmapdb import CMapDB
from pdfminer.layout import LAParams
import xml.dom.minidom
import re
import json
reload(sys)
sys.setdefaultencoding('utf-8')

logger = get_logger(__file__)

CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))
FLIE_PATH = os.path.join(CURRENT_PATH,'pdfFile_bcflvyjs')
PDF_FILE_NAME = '0b64e23327d865518fe80091753c1f6ef4921760329b59771da0dce089acb3d1.pdf'
XML_FILE_NAME = PDF_FILE_NAME.split('.')[0] + '.xml'
PDF_FILE = os.path.join(FLIE_PATH,PDF_FILE_NAME)
XML_FILE = os.path.join(FLIE_PATH,XML_FILE_NAME)

class pdfConert(object):

    def __init__(self,pdf_file,xml_file):
        self.fp = file(pdf_file,'rb')
        self.outfp = file(xml_file,'w')

    def doConvert(self):
        """
        转换 pdf 到指定文本格式
        :return:
        """
        # debug option
        debug = 0
        # input option
        password = ''
        pagenos = set()
        maxpages = 0
        # output option
        outfile = XML_FILE_NAME
        outtype = ''
        imagewriter = None
        rotation = 0
        layoutmode = 'normal'
        codec = 'utf-8'
        scale = 1
        caching = True
        laparams = LAParams()


        PDFDocument.debug = debug
        PDFParser.debug = debug
        CMapDB.debug = debug
        PDFResourceManager.debug = debug
        PDFPageInterpreter.debug = debug
        PDFDevice.debug = debug
        #

        device = None
        rsrcmgr = PDFResourceManager(caching=caching)
        if not outtype:
            outtype = 'text'
            if outfile:
                if outfile.endswith('.htm') or outfile.endswith('.html'):
                    outtype = 'html'
                elif outfile.endswith('.xml'):
                    outtype = 'xml'
                elif outfile.endswith('.tag'):
                    outtype = 'tag'
        if outfile:
            outfp = self.outfp
        else:
            outfp = sys.stdout
        if outtype == 'text':
            device = TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
                                   imagewriter=imagewriter)
        elif outtype == 'xml':
            device = XMLConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
                                  imagewriter=imagewriter)
        elif outtype == 'html':
            device = HTMLConverter(rsrcmgr, outfp, codec=codec, scale=scale,
                                   layoutmode=layoutmode, laparams=laparams,
                                   imagewriter=imagewriter)
        elif outtype == 'tag':
            device = TagExtractor(rsrcmgr, outfp, codec=codec)


        fp = self.fp
        interpreter = PDFPageInterpreter(rsrcmgr, device)
        for page in PDFPage.get_pages(fp, pagenos,
                                      maxpages=maxpages, password=password,
                                      caching=caching, check_extractable=True):
            page.rotate = (page.rotate+rotation) % 360


            # decode('latin1')
            interpreter.process_page(page)
        fp.close()
        device.close()
        outfp.close()


    def read_xml(self):
        """
        读取转换后的xml数据
        :return:
        """

        dom = xml.dom.minidom.parse(XML_FILE)
        root = dom.documentElement
        pages = root.getElementsByTagName('page')

        #页中的文本行数据
        pageTextLine = []

        for page in pages:
            lineInfoList = []
            textboxs = page.getElementsByTagName('textbox')
            for textbox in  textboxs:
                self.textline_put_in_order(textbox, lineInfoList)


            # 根据行坐标排序
            lineInfoList.sort(key=lambda x:x['positionShape'].top, reverse=True)

            pageTextLine.append(self.convert_ordered_lineInfoList_to_lineTextList(lineInfoList))

if __name__ == '__main__':
    convert_test = pdfConert(PDF_FILE,XML_FILE)
    convert_test.doConvert()
    convert_test.read_xml()